﻿Option Explicit On

'TWILT - Tweet What I'm Listening To - Coded by -DefaulT
'Copyright (C) 2011  -DefaulT

'Distributed Through http://twilt.co.cc/

'This program is free software; you can redistribute it and/or
'modify it under the terms of the GNU General Public License
'as published by the Free Software Foundation; either version 2
'of the License, or (at your option) any later version.

'This program is distributed in the hope that it will be useful,
'but WITHOUT ANY WARRANTY; without even the implied warranty of
'MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'GNU General Public License for more details.

'You should have received a copy of the GNU General Public License
'along with this program; if not, write to the Free Software
'Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

Public Class frmMain

    Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

    ' Dragging form w/o borders
#Region " ClientAreaMove Handling "
    Const WM_NCHITTEST As Integer = &H84
    Const HTCLIENT As Integer = &H1
    Const HTCAPTION As Integer = &H2
    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        Select Case m.Msg
            Case WM_NCHITTEST
                MyBase.WndProc(m)
                If m.Result = HTCLIENT Then m.Result = HTCAPTION
            Case Else
                MyBase.WndProc(m)
        End Select
    End Sub
#End Region

    Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LoadMain()
        LoadPlaylist()
    End Sub

    ' Open new media button.
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        OpenFileDialog1.ShowDialog()
    End Sub

    ' We selected our media.
    Private Sub OpenFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
        For Each track As String In OpenFileDialog1.FileNames
            ListBox3.Items.Add(track)
            If WMPLib.WMPPlayState.wmppsPlaying = True Then
                ListBox3.SelectedIndex = ListBox3.SelectedIndex + 1
            Else
                ListBox3.SelectedIndex = 0
            End If
        Next
    End Sub

    ' New media added, lets start playing it, and update some stuff.
    Private Sub ListBox3_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox3.SelectedIndexChanged
        NewMedia()
        SongHistoryLV()
        Sleep(1000)
        NewRefresh()
    End Sub

    ' Send your own tweet.
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        ManualTweet()
        Sleep(2000)
        NewRefresh()
    End Sub

    ' Update character counter.
    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        Label8.Text = -Val(TextBox1.Text.Length) + 140
    End Sub

    ' Close application.
    Private Sub PictureBox2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox2.Click
        frmLogin.Close()
        Form3.Close()
        frmBrowser.Close()
        Me.Close()
    End Sub

    ' Kep updating labels with song information.
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        LabelUpdate()
    End Sub

    ' Play Button.
    Private Sub PictureBox6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox6.Click
        If WMPLib.WMPPlayState.wmppsPlaying = True Then
            'Do nothing
        Else
            AxWindowsMediaPlayer1.Ctlcontrols.play()
        End If
    End Sub

    ' Skip track back.
    Private Sub PictureBox5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox5.Click
        If ListBox3.SelectedIndex <> 0 Then
            ListBox3.SelectedIndex = ListBox3.SelectedIndex - 1
        End If
    End Sub

    ' Pause Button.
    Private Sub PictureBox7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox7.Click
        If WMPLib.WMPPlayState.wmppsPaused = True Then
            'Do nothing
        Else
            AxWindowsMediaPlayer1.Ctlcontrols.pause()
        End If
    End Sub

    ' Stop Button.
    Private Sub PictureBox8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox8.Click
        If WMPLib.WMPPlayState.wmppsStopped = True Then
            'Do nothing
        Else
            AxWindowsMediaPlayer1.Ctlcontrols.stop()
        End If
    End Sub

    ' Skip forward.
    Private Sub PictureBox9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox9.Click
        If ListBox3.SelectedIndex < ListBox3.Items.Count - 1 Then
            ListBox3.SelectedIndex = ListBox3.SelectedIndex + 1
        End If

    End Sub

    ' Refresh Button
    Private Sub PictureBox10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox10.Click
        NewRefresh()
    End Sub

    ' About
    Private Sub PictureBox12_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox12.Click
        Form6.Show()
    End Sub

    'Context menu strip show.
    Private Sub NotifyIcon1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles NotifyIcon1.MouseClick
        If e.Button = MouseButtons.Right Then
            NotifyIcon1.ContextMenuStrip.Show()
        End If
    End Sub

    ' Context close.
    Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
        frmLogin.Close()
        frmBrowser.Close()
        Form3.Close()
        Me.Close()
    End Sub

    ' Context donate.
    Private Sub DonateToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DonateToolStripMenuItem.Click
        Process.Start("https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=T95S2B7T4YJF2")
    End Sub

    ' Context about.
    Private Sub AboutToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AboutToolStripMenuItem.Click
        Form6.Show()
    End Sub

    ' Donate.
    Private Sub PictureBox13_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox13.Click
        Process.Start("https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=T95S2B7T4YJF2")
    End Sub

    ' Minimize.
    Private Sub PictureBox11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox11.Click
        Me.Hide()
    End Sub

    ' Un-minimize?
    Private Sub ShowToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ShowToolStripMenuItem.Click
        Me.Show()
    End Sub

    ' Auto refresh.
    Private Sub Timer2_Tick(sender As System.Object, e As System.EventArgs) Handles Timer2.Tick
        NewRefresh()
    End Sub

    ' Submit bug.
    Private Sub PictureBox15_Click(sender As System.Object, e As System.EventArgs) Handles PictureBox15.Click
        Process.Start("http://code.google.com/p/twilt/issues/entry")
    End Sub

    ' Save the playlist (listbox3).
    Private Sub PictureBox16_Click(sender As System.Object, e As System.EventArgs) Handles PictureBox16.Click
        SavePlaylist()
    End Sub

    ' Delete a song from the playlist.
    Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
        DeleteSong()
    End Sub

    ' Show them our twitter.
    Private Sub PictureBox17_Click(sender As System.Object, e As System.EventArgs) Handles PictureBox17.Click
        Process.Start("https://twitter.com/TwiltMedia")
    End Sub

End Class